package com.sskj.app.controller;


import com.fegin.bean.app.StockUser;
import com.fegin.client.app.StockUserFegin;
import com.sskj.app.controller.commen.BaseController;
import com.sskj.app.util.cacheUtil.JedisUtil;
import com.util.Respons.ResponseUtil;
import com.util.auth.AuthSign;
import com.util.exception.CommonException;
import io.swagger.annotations.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 手机用户表 前端控制器
 * </p>
 *
 * @author 张子艺
 * @since 2019-02-19
 */
@Controller
@RequestMapping("/stockUser")
@Api(value = "App用户controller", tags = {"用户操作接口"})
public class StockUserController extends BaseController {
    @Autowired
    StockUserFegin stockUserFegin;
    @Autowired
    JedisUtil jedisUtil;

    /**
     * 用户登陆
     * ApiResponse注解 生成文档所用 如果返回结果内含有实体类 message="实体类名称"  如果返回结果为自定义 message="自定义结果"
     * @param account 账号
     * @param pswd    密码
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "用户登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pswd", value = "密码", dataType = "String", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 200,message ="模拟Token：SABDKBAKUBDKABS415D1AF5C1SD51"  )
    })
    public Map login(String account, String pswd) {
        if (StringUtils.isNotBlank(account) && StringUtils.isNotBlank(pswd)) {
            return stockUserFegin.login(account, pswd);
        } else {
            return ResponseUtil.getNotNormalMap("参数校验错误");
        }

    }

    /**
     * 修改用户信息
     * 修改    ID必传 Token必须传  其余参数 修改哪个字段 传哪个字段参数
     *
     * @param stockUser 用户信息
     * @return
     */
    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = " 修改用户信息</br>" +
            " 修改  ID必传 Token必须传 其余参数 修改哪个字段 传哪个字段参数</br>" +
            "(必须当前用户修改当前用户的信息)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "Token", dataType = "String", paramType = "header" ,required = true)
    })
    @ApiResponses({
            @ApiResponse(code = 200,message ="StockUser"  )
    })
    Map<String, Object> updateUser(@RequestBody StockUser stockUser, @RequestHeader("token") String token) {
        //   校验token中的Id 是否和参数Id相同  防止窃取Token去修改其他用户的信息
        //获取令牌中的userId和userName
        Map map = AuthSign.verifyToken(token);
        Long tokenUserId = Long.parseLong(map.get("userId") + "");
        //比较令牌中的usrId和传入的userId是否相同
        if (tokenUserId.longValue()==stockUser.getId().longValue()) {
            return stockUserFegin.insertOrUpdate(stockUser);
        } else {
            return ResponseUtil.getTokenOrParamsErrResponseMap();
        }
    }

    /**
     * 新增用户
     * 新增  ID必须为空 Token必须为空
     *
     * @param stockUser 用户信息
     * @return
     */
    @RequestMapping(value = "/insertUser", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增用户 </br>" +
            "新增  ID必须为空 Token必须为空")
    Map<String, Object> insertUser(@RequestBody StockUser stockUser) {
        return stockUserFegin.insertOrUpdate(stockUser);
    }

    /**
     * 测试Token
     *
     * @return
     */
    @RequestMapping(value = "/TokenTest", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "测试Token")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "Token", dataType = "String", paramType = "header" ,required = true)
    })
    public Map TokenTest(@RequestHeader("token") String token) {
        System.err.println(token);
        if (AuthSign.verify(token)) {
            return ResponseUtil.getSuccessMap("认证成功");
        } else {
            throw new CommonException("无效Token或Token已过期");
        }

    }

}
